﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DCOutwardSaleQty.aspx.cs" Inherits="Main_Inventory_DCOutwardSaleQty" Culture="en-GB" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControl" %>
<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Quantity </title>
     <link href="../../Styles/accounts.css" rel="stylesheet" type="text/css" />
    <script src="../../JavaScript/Commons.js" type="text/javascript"></script>
 <script type="text/javascript" language="javascript">
     function isNumberKey(control_id, evt) {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 45 || charCode > 47))
             return false;

         if (charCode == 46) {
             var dotcount = document.getElementById(control_id).value.split('.').length - 1;
             if (dotcount >= 1) {
                 return false;
             }
         }
         return true;
     }
     function calendarShown(sender, args) {
         sender._popupBehavior._element.style.zIndex = 100005;
     }
     
     function RemoveZero(id) {
         var ctrl = document.getElementById(id);
         if (ctrl.value == '0.00') {
             ctrl.value = '';
         }
     }

     function CalculateQty() {
         var totalQty = 0;
         var count = document.getElementById("hidControlCounter").value;
         for (var i = 1; i <= count; i++) {
             var txtSaleQty = document.getElementById("txtSaleQty" + i);
             if (txtSaleQty.value == '') {
                 txtSaleQty.value = '0.00';
             }
             else if (parseFloat(txtSaleQty.value) > 0) {
                 totalQty += parseFloat(txtSaleQty.value);
             }
         }
         document.getElementById("txtTotalQty").value = parseFloat(totalQty).toFixed(2);
     }
     function CloseWindow() {
         window.close();
     }

     function ClearTextbox() {
         document.getElementById("lblMsgBatch").innerHTML = '';
         document.getElementById("txtBatchNo").value = '';
         if (document.getElementById("hidWarrantyEnabled").value == 'False') {
             document.getElementById("txtMFDDate").value = '';
             document.getElementById("txtExpiryDate").value = '';
         }
         document.getElementById("txtRemarks").value = '';
     }
     function ReturnSaleQuantity()
      {
         if (isEmptyDropdownList() && Validation()) {
             var count = document.getElementById("hidControlCounter").value;
             var hidbatchEnabled = document.getElementById("hidbatchEnabled");
             var hidWarrantyEnabled = document.getElementById("hidWarrantyEnabled");
             var LocationIdandQty = '';
             var totalQty = 0;
             if (hidbatchEnabled.value == 'True' && hidWarrantyEnabled.value == 'True') {// if batch & warranty
                 for (var i = 1; i <= count; i++) {
                     var txtSaleQty = document.getElementById("txtSaleQty" + i);
                     var ddlLocation = document.getElementById("ddlLocation" + i);
                     var ddlBatch = document.getElementById("ddlBatch" + i);
                     var txtWarranty = document.getElementById("txtWarranty" + i);
                     var ddlWarrantyDetails = document.getElementById("ddlWarrantyDetails" + i);
                     var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                     var BatchId = ddlBatch.options[ddlBatch.selectedIndex].value;
                     var WarrantyId = ddlWarrantyDetails.options[ddlWarrantyDetails.selectedIndex].value;
                     if (parseFloat(txtSaleQty.value) > 0) {
                         totalQty += parseFloat(txtSaleQty.value);
                         if (i == 1) {
                             LocationIdandQty += LocationId + ',' + txtSaleQty.value + '|' + BatchId + ':' + txtWarranty.value + ';' + WarrantyId;
                         }
                         else {
                             LocationIdandQty += '/' + LocationId + ',' + txtSaleQty.value + '|' + BatchId + ':' + txtWarranty.value + ';' + WarrantyId;
                         }
                     }
                 }
             }
             else if (hidbatchEnabled.value == 'True' && hidWarrantyEnabled.value == 'False') {// if only batch enabled
                 for (var i = 1; i <= count; i++) {
                     var txtSaleQty = document.getElementById("txtSaleQty" + i);
                     var ddlLocation = document.getElementById("ddlLocation" + i);
                     var ddlBatch = document.getElementById("ddlBatch" + i);
                     var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                     var BatchId = ddlBatch.options[ddlBatch.selectedIndex].value;
                     if (parseFloat(txtSaleQty.value) > 0) {
                         totalQty += parseFloat(txtSaleQty.value);
                         if (i == 1) {
                             LocationIdandQty += LocationId + ',' + txtSaleQty.value + '|' + BatchId;
                         }
                         else {
                             LocationIdandQty += '/' + LocationId + ',' + txtSaleQty.value + '|' + BatchId;
                         }
                     }
                 }
             }
             else if (hidbatchEnabled.value == 'False' && hidWarrantyEnabled.value == 'False') {// not batch & not warranty
                 for (var i = 1; i <= count; i++) {
                     var txtSaleQty = document.getElementById("txtSaleQty" + i);
                     var ddlLocation = document.getElementById("ddlLocation" + i);
                     var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                     if (parseFloat(txtSaleQty.value) > 0) {
                         totalQty += parseFloat(txtSaleQty.value);
                         if (i == 1) {
                             LocationIdandQty += LocationId + ',' + txtSaleQty.value;
                         }
                         else {
                             LocationIdandQty += '/' + LocationId + ',' + txtSaleQty.value;
                         }
                     }
                 }
             }
             if (parseFloat(totalQty) > 0) {
                 var hidLocationCtrlId = document.getElementById('hidLocationCtrlId').value;
                 var hidRequestCtrl = document.getElementById('hidRequestCtrl').value;

                 window.opener.document.getElementById(hidLocationCtrlId).value = LocationIdandQty;
                 window.opener.document.getElementById(hidRequestCtrl).value = parseFloat(totalQty);
                 //window.opener.CalculateAmtBillLevel();
                 self.close();
                 return false;
             }
             else {
                 alert('Total Quantity is Zero');
                 return false;
             }
         }
     }
     function isEmptyDropdownList() {
         var flag = true;
         var hidbatchEnabled = document.getElementById("hidbatchEnabled");
         var ddlLocation = document.getElementById("ddlLocation1");
         if (ddlLocation.options.length == 0) {
             flag = false;
             alert('Create Location');
         }
         if (hidbatchEnabled.value == 'True') {
             var ddlBatch = document.getElementById("ddlBatch1");
             if (ddlBatch.options.length == 0) {
                 flag = false;
                 alert('Create Batch');
             }
         }
         return flag;
     }

     function Validation() {
         var count = document.getElementById("hidControlCounter").value;
         var hidbatchEnabled = document.getElementById("hidbatchEnabled");
         var hidWarrantyEnabled = document.getElementById("hidWarrantyEnabled");
         var flag = true;
         if (hidbatchEnabled.value == 'True' && hidWarrantyEnabled.value == 'True') {// if batch & warranty
             for (var i = 1; i <= count; i++) {
                 var txtSaleQty = document.getElementById("txtSaleQty" + i);
                 var ddlLocation = document.getElementById("ddlLocation" + i);
                 var ddlBatch = document.getElementById("ddlBatch" + i);
                 var txtWarranty = document.getElementById("txtWarranty" + i);
                 var ddlWarrantyDetails = document.getElementById("ddlWarrantyDetails" + i);
                 var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                 var BatchId = '0';
                 if (ddlBatch.options.length > 0) {
                     BatchId = ddlBatch.options[ddlBatch.selectedIndex].value; ;
                 }
                 var WarrantyId = ddlWarrantyDetails.options[ddlWarrantyDetails.selectedIndex].value;
                 if (parseFloat(txtSaleQty.value) > 0) {
                     if (LocationId == '0') {
                         alert('Select Location at row no. ' + i);
                         flag = false;
                     }
                     else if (BatchId == '0') {
                         alert('Select Batch at row no. ' + i);
                         flag = false;
                     }
                     else if (txtWarranty.value == '') {
                         alert('Specify Warranty at row no. ' + i);
                         flag = false;
                     }
                 }
             }
         }
         else if (hidbatchEnabled.value == 'True' && hidWarrantyEnabled.value == 'False') {// if only batch enabled
             for (var i = 1; i <= count; i++) {
                 var ddlLocation = document.getElementById("ddlLocation" + i);
                 var ddlBatch = document.getElementById("ddlBatch" + i);
                 var txtSaleQty = document.getElementById("txtSaleQty" + i);
                 var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                 var BatchId = '0';
                 if (ddlBatch.options.length > 0) {
                     BatchId = ddlBatch.options[ddlBatch.selectedIndex].value; ;
                 }

                 if (parseFloat(txtSaleQty.value) > 0) {
                     if (LocationId == '0') {
                         alert('Select Location at row no. ' + i);
                         flag = false;
                     }
                     else if (BatchId == '0') {
                         alert('Select Batch at row no. ' + i);
                         flag = false;
                     }
                 }
             }
         }
         else if (hidbatchEnabled.value == 'False' && hidWarrantyEnabled.value == 'False') {
             for (var i = 1; i <= count; i++) {// not batch & not warranty
                 var txtSaleQty = document.getElementById("txtSaleQty" + i);
                 var ddlLocation = document.getElementById("ddlLocation" + i);
                 var txtSaleQty = document.getElementById("txtSaleQty" + i);
                 var LocationId = ddlLocation.options[ddlLocation.selectedIndex].value;
                 if (parseFloat(txtSaleQty.value) > 0) {
                     if (LocationId == '0') {
                         alert('Select Location at row no. ' + i);
                         flag = false;
                     }
                 }
             }
         }
         return flag;
     }
        
    </script>
</head>

<body>
    <form id="form1" runat="server">
        <AjaxControl:ToolkitScriptManager ID="ScriptManager1" runat="server"> </AjaxControl:ToolkitScriptManager>
    <%-- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>   --%>
        <asp:HiddenField ID="hidItemId" runat="server" />
        <asp:HiddenField ID="hidRequestCtrl" runat="server" />        
        <asp:HiddenField ID="hidLocationCtrlId" runat="server" />
        <asp:HiddenField ID="hidLocationIdandQty" runat="server" />
        <asp:HiddenField ID="hidControlCounter" runat="server" Value="1"/>
        <asp:HiddenField ID="hidbatchEnabled" runat="server" /> 
        <asp:HiddenField ID="hidWarrantyEnabled" runat="server" />       
        <center>
            <br />            
                <table class="tablecontent" style="width:97%; margin-left:1%; margin-right:1%;" cellspacing="0">
                    <tr>
                        <td class="headtd">
                            Recieved Quantity
                        </td>
                    </tr>
                    <tr>
                        <td style="height:100px; vertical-align:top;"> 
                            <table id="tblItem" runat="server" class="tablecontent" style="border:solid 1px #818181; width:100%;" cellpadding="2" cellspacing="0">
                                <tr>
                                    <th style="width:20%;border-bottom:solid 1px #818181; border-right:solid 1px #818181">
                                        Locations
                                    </th> 
                                    <th id="tdBatch0" runat="server" style="width:20%; border-bottom:solid 1px #818181; border-right:solid 1px #818181">
                                        Batch No.
                                    </th> 
                                    <th style="width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181">
                                        Balance Qty.
                                    </th> 
                                    <th style=" width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181">
                                        Primary Unit&nbsp; Qty.
                                    </th> 
                                    <th id="tdWarranty0" runat="server" style="width:15%; border-bottom:solid 1px #818181; border-right:solid 1px #818181">
                                        Warranty
                                    </th> 
                                    <th id="tdWarrantyDetails0" runat="server" style="width:15%; border-bottom:solid 1px #818181;">
                                        Warranty Details
                                    </th>                                                                       
                                </tr>
                                <tr>
                                    <td>
                                        <asp:DropDownList ID="ddlLocation1" runat="server" CssClass="dropdown" Width="98%"></asp:DropDownList>
                                    </td>
                                    <td id="tdBatch1" runat="server">
                                        <asp:DropDownList ID="ddlBatch1" runat="server" CssClass="dropdown" Width="98%"></asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblBalanceQty1" runat="server" CssClass="labelNumber" Width="98%"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtSaleQty1" runat="server" CssClass="inputnumber" onkeypress="return isNumberKey(this.id,event);" onfocus="return RemoveZero(this.id);" onblur="return CalculateQty();" Width="98%" Text="0.00"></asp:TextBox>
                                    </td>
                                    <td id="tdWarranty1" runat="server">
                                        <asp:TextBox ID="txtWarranty1" runat="server" CssClass="inputnumber" onkeypress="return isNumberKey(this.id,event);" Width="98%"></asp:TextBox>
                                    </td>
                                    <td id="tdWarrantyDetails1" runat="server">
                                        <asp:DropDownList ID="ddlWarrantyDetails1" runat="server" CssClass="dropdown" Width="98%">
                                            <asp:ListItem Text="Months" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="Years" Value="0"></asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table style="border:solid 1px #818181; width:100%; background-color:#EAF2F2;">
                                <tr>
                                    <td align="left">
                                        <asp:Button ID="btnAddMore" runat="server" CssClass="button" Text="Add More" OnClick="btnAddMore_Click" />
                                    </td>
                                    <td align="right">
                                        Total Sale Quantity :
                                    </td>
                                    <td align="left">
                                        <asp:TextBox ID="txtTotalQty" runat="server" Text="0.00" CssClass="inputnumber"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left">
                                        <asp:LinkButton ID="btnBatch" runat="server" Text="Create Batch"></asp:LinkButton>
                                    </td>
                                    <td align="right">
                                        <asp:Button ID="btnOk" runat="server" Text="OK" CssClass="button" OnClientClick="return ReturnSaleQuantity();" />
                                    </td>
                                    <td align="left">
                                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="button" OnClientClick="return CloseWindow();" />
                                    </td>
                                </tr>
                            </table>                        
                        </td>
                    </tr> 
                </table>
                   
        </center>
     
            
            <asp:Panel ID="panelBatch" runat="server" Width="60%" style="display:none; height:210px" CssClass="modalPopup">
                <div style="float:right;">
                    <asp:LinkButton ID="lnkBtnClose" runat="server" Text="Close" OnClientClick="return ClearTextbox();"></asp:LinkButton>
                </div>
                <center>
                    <asp:Label ID="lblMsgBatch" runat="server" CssClass="false"></asp:Label>
                </center>
                <table style="width:80%; margin-top:35px; margin-right:10%; margin-left:10%;">
                    <tr>
                        <td style="width:40%">
                            Batch No.
                        </td>
                        <td style="width:60%">
                            <asp:TextBox ID="txtBatchNo" runat="server" CssClass="input" ValidationGroup="V"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqBatchNo" runat="server" ControlToValidate="txtBatchNo" ValidationGroup="V" ErrorMessage="*"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                     <tr id="trMFDDate" runat="server">
                        <td>
                            Manufactured Date
                        </td>
                        <td>
                            <asp:TextBox ID="txtMFDDate" runat="server" CssClass="input" ValidationGroup="V"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqMFDDate" runat="server" ControlToValidate="txtMFDDate" ValidationGroup="V" ErrorMessage="*"></asp:RequiredFieldValidator>
                            <AjaxControl:CalendarExtender ID="calMFDDate" runat="server"  TargetControlID="txtMFDDate" OnClientShown="calendarShown" Format="dd/MM/yyyy"></AjaxControl:CalendarExtender>
                            <AjaxControl:MaskedEditExtender ID="maskMFDDate" runat="server" Mask="99/99/9999" CultureName="en-GB" MaskType="Date" TargetControlID="txtMFDDate"></AjaxControl:MaskedEditExtender>
                            <AjaxControl:MaskedEditValidator ID="valMFDDate" runat="server" ControlExtender="maskMFDDate" ValidationGroup="V" ControlToValidate="txtMFDDate" ToolTip="DD/MM/YYYY" InvalidValueMessage="Invalid Date"></AjaxControl:MaskedEditValidator>
                            
                        </td>
                    </tr>
                     <tr id="trExpiryDate" runat="server">
                        <td>
                            Expiry Date
                        </td>
                        <td>
                            <asp:TextBox ID="txtExpiryDate" runat="server" CssClass="input" ValidationGroup="V"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqExpiryDate" runat="server" ControlToValidate="txtExpiryDate" ValidationGroup="V" ErrorMessage="*"></asp:RequiredFieldValidator>
                            <AjaxControl:CalendarExtender ID="calExpiryDate" runat="server" TargetControlID="txtExpiryDate" OnClientShown="calendarShown" Format="dd/MM/yyyy"></AjaxControl:CalendarExtender>
                            <AjaxControl:MaskedEditExtender ID="maskExpiryDate" runat="server" Mask="99/99/9999" CultureName="en-GB" MaskType="Date" TargetControlID="txtExpiryDate"></AjaxControl:MaskedEditExtender>
                            <AjaxControl:MaskedEditValidator ID="valExpiryDate" runat="server" ValidationGroup="V" ControlExtender="maskExpiryDate" ControlToValidate="txtExpiryDate" ToolTip="DD/MM/YYYY" InvalidValueMessage="Invalid Date"></AjaxControl:MaskedEditValidator>
                            
                        </td>
                    </tr>
                     <tr>
                        <td>
                            Remarks
                        </td>
                        <td>
                            <asp:TextBox ID="txtRemarks" runat="server" CssClass="input"></asp:TextBox>
                        </td>
                    </tr>
                     <tr>
                        <td>
                        
                        </td>
                        <td>
                            <asp:Button ID="btnSaveBatch" runat="server" CssClass="button" Text="Save" ValidationGroup="V" OnClick="btnSaveBatch_Click" /> 
                        </td>
                    </tr>
                
                </table>
            </asp:Panel>
            <AjaxControl:ModalPopupExtender ID="modalBatch" runat="server" PopupControlID="panelBatch" TargetControlID="btnBatch" BackgroundCssClass="modalBackground" CancelControlID="lnkBtnClose" ></AjaxControl:ModalPopupExtender>            
   <%--     </ContentTemplate>
        </asp:UpdatePanel>--%>
    </form>
</body>
</html>
